package com.wl.cloud.core.exception;

import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.core.utils.RestResultUtil;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;
import java.util.concurrent.RejectedExecutionException;
import java.util.stream.Collectors;

/**
 * @author: wanglin
 * @date: 2023-07-12 周三
 * @Version: 1.0
 * @Description:
 */
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public RestResultDTO<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return RestResultUtil.handleException(e, "json数据格式不正确,无法解析");
    }

//    @ExceptionHandler(MissingServletRequestParameterException.class)
//    public RestResultDTO<?> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
//        String parameterName = e.getParameterName();
//        return RestResultUtil.handleException(e, Objects.equals(parameterName, "tenantId") ? "租户ID不能为空" : parameterName + "不能为空");
//    }

    @ExceptionHandler(BindException.class)
    public RestResultDTO<?> handleBindException(BindException e) {
        String msgStr = e.getBindingResult().getFieldErrors().stream().map(
                fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage()
        ).collect(Collectors.joining(", "));
        return RestResultUtil.handleException(e, msgStr);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public RestResultDTO<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String msgStr = e.getBindingResult().getFieldErrors().stream().map(
                fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage()
        ).collect(Collectors.joining(", "));
        return RestResultUtil.handleException(e, msgStr);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public RestResultDTO<?> handleConstraintViolationException(ConstraintViolationException e) {
        String msgStr = e.getConstraintViolations().stream().map(constraintViolation -> {
            PathImpl pathImpl = (PathImpl) constraintViolation.getPropertyPath();
            String paramName = pathImpl.getLeafNode().getName();
            String message = constraintViolation.getMessage();
            return paramName + ": " + message;
        }).collect(Collectors.joining(", "));
        return RestResultUtil.handleException(e, msgStr);
    }

    @ExceptionHandler(Exception.class)
    public RestResultDTO<?> handleException(Exception e) {
        return RestResultUtil.handleException(e, null);
    }

    @ExceptionHandler(RejectedExecutionException.class)
    public RestResultDTO<?> handleRejectedExecutionException(RejectedExecutionException e) {
        return RestResultUtil.handleException(e, "服务器正忙，请稍后再试！");
    }

}
